package com.xx.eshop.controller;

import java.awt.Image;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xx.eshop.entity.Attr;
import com.xx.eshop.entity.AttrValue;
import com.xx.eshop.entity.Product;
import com.xx.eshop.entity.ProductAttrValue;
import com.xx.eshop.entity.ProductImage;
import com.xx.eshop.service.AttrService;
import com.xx.eshop.service.AttrValueService;
import com.xx.eshop.service.CategoryService;
import com.xx.eshop.service.ProductAttrValueService;
import com.xx.eshop.service.ProductImageService;
import com.xx.eshop.service.ProductService;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductImageService productImageService;
	
	@Autowired
	private ProductAttrValueService productAttrValueService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private AttrService attrService;
	
	@Autowired
	private AttrValueService attrValueService;

	// 修改商品的所有信息
	@RequestMapping("/admin/product/update")
	@ResponseBody
	public Map<String, Object> updateProduct(Product product, @RequestParam List<String> images,
			@RequestParam List<String> attr_names, @RequestParam List<String> values){
		System.out.println("----进入了商品信息修改的controller-------");
		Map<String, Object> map = new HashMap<>();
		System.out.println(product);
		System.out.println(images);
		System.out.println(attr_names);
		System.out.println(values);
		productService.update(product, images, attr_names, values);
		map.put("success", true);
		return map;
		
	}
	
	@RequestMapping("/admin/product/delete")
	@ResponseBody
	public Map<String, Object> productDelete(int product_id){
		Map<String, Object> map = new HashMap<String, Object>();
		// 删除属性名属性值信息
		productAttrValueService.delete(product_id);
		// 删除图片信息
		productImageService.delete(product_id);
		// 删除商品信息
		productService.delete(product_id);
		map.put("success", true);
		return map;
		
	}
	
	
	// 根据商品id查询商品图片信息
	@RequestMapping("/admin/product/findProductImage")
	@ResponseBody
	public List<ProductImage> findProductImage(int product_id){
		//System.out.println("findProductImage-----"+product_id);
		return productImageService.findByProductId(product_id);
	}
	
	// 已知 类别id 商品id  查询有关商品的所有信息
	@RequestMapping("/admin/product/findProductAll")
	@ResponseBody
	public List<ProductAttrValue> findProductAttrAndValues(Integer product_id){
		return productAttrValueService.findByProductId(product_id);
	}
	
	
	/*@RequestMapping("/admin/product/save")
	@ResponseBody
	public Map<String, Object> save(Product product, @RequestParam List<ProductImage> images,
			@RequestParam List<Attr> attr_names, @RequestParam List<ProductAttrValue> values) {
		System.out.println("1111111111111111111111111----进入了productController的save方法");
		Map<String, Object> map = new HashMap<>();
		System.out.println(product);
		System.out.println(images);
		System.out.println(attr_names);
		System.out.println(values);
		
		for(ProductImage img:images){
			System.out.println("==============");
			System.out.println("=============="+img.getProduct_id());
			//productImageService.save(img);
		}
		
		
		productService.save(product, images, attr_names, values);
		map.put("success", true);
		return map;
	}*/
	
	
	@RequestMapping("/admin/product/index")
	public String index() {
		return "/admin/product/index";
	}

	@RequestMapping("/admin/product/findByPage")
	@ResponseBody
	public Map<String, Object> findByPage(String name, Double minPrice, Double maxPrice, Boolean onSale, int page,
			int rows, String sort, String order) {
		return productService.findByPage(name, minPrice, maxPrice, onSale, page, rows, sort, order);
	}

	@RequestMapping("/admin/product/save")
	@ResponseBody
	public Map<String, Object> save(Product product, @RequestParam List<String> images,
			@RequestParam List<String> attr_names, @RequestParam List<String> values) {
		System.out.println("1111111111111111111111111----进入了productController的save方法");
		Map<String, Object> map = new HashMap<>();
		System.out.println(product);
		System.out.println(images);
		System.out.println(attr_names);
		System.out.println(values);
		productService.save(product, images, attr_names, values);
		map.put("success", true);
		return map;
	}

}
